/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/gfx/ImageShape.h"

namespace alkes {

ImageShape::ImageShape(uint32_t num)
: Shape(num)
{
    rects_.initialize(num);
}

ImageShape::~ImageShape()
{
}

const Rect ImageShape::getSrcRect(uint32_t index) const
{
    AL_PRE_COND(validate(index), Rect::ZERO);

    return rects_[index].src_rect;
}

const Rect ImageShape::getDestRect(uint32_t index) const
{
    AL_PRE_COND(validate(index), Rect::ZERO);

    return rects_[index].dst_rect;
}

const Color ImageShape::getColor(uint32_t index) const
{
    AL_PRE_COND(validate(index), Color::WHITE);

    return rects_[index].color;
}

const ImageShape::ImageRect& ImageShape::getImageRect(uint32_t index) const
{
    return rects_[index];
}

const Image& ImageShape::getImage() const
{
    return image_;
}

Image* ImageShape::getImagePtr()
{
    return &image_;
}

bool ImageShape::hasImageBuffer() const
{
    return 0 != getImage().getImageBuffer();
}

void ImageShape::setImage(Image& image)
{
    image_ = image;
    setDirty(true);
}

void ImageShape::setImage(ImageBuffer* buffer)
{
    image_.setImageBuffer(buffer);
    setDirty(true);
}

void ImageShape::setImage(ImageBuffer* buffer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
{
    image_.setImageBuffer(buffer, x, y, width, height);
    setDirty(true);
}

void ImageShape::setParam(const Point2D& dst_xy, const Color& color)
{
    setParam(0, dst_xy, color);
}

void ImageShape::setParam(const Rect& dst_rect, const Color& color)
{
    setParam(0, dst_rect, color);
}

void ImageShape::setParam(const Rect& src_rect, const Point2D& dst_xy, const Color& color)
{
    setParam(0, src_rect, dst_xy, color);
}

void ImageShape::setParam(const Rect& src_rect, const Rect& dst_rect, const Color& color)
{
    setParam(0, src_rect, dst_rect, color);
}

void ImageShape::setSrcRect(const Rect* src_rect)
{
    setSrcRect(0, src_rect);
}

void ImageShape::setDest(const Point2D& dst_xy)
{
    setDest(0, dst_xy);
}

void ImageShape::setDest(const Rect& dst_rect)
{
    setDest(0, dst_rect);
}

void ImageShape::setColor(const Color& color)
{
    setColor(0, color);
}

void ImageShape::setParam(uint32_t index, const Point2D& dst_xy, const Color& color)
{
    setSrcRect(index, 0);
    setDest(index, dst_xy);
    setColor(index, color);
}

void ImageShape::setParam(uint32_t index, const Rect& dst_rect, const Color& color)
{
    setSrcRect(index, 0);
    setDest(index, dst_rect);
    setColor(index, color);
}

void ImageShape::setParam(uint32_t index, const Rect& src_rect, const Point2D& dst_xy, const Color& color)
{
    setSrcRect(index, &src_rect);
    setDest(index, dst_xy);
    setColor(index, color);
}

void ImageShape::setParam(uint32_t index, const Rect& src_rect, const Rect& dst_rect, const Color& color)
{
    setSrcRect(index, &src_rect);
    setDest(index, dst_rect);
    setColor(index, color);
}

void ImageShape::setSrcRect(uint32_t index, const Rect* src_rect)
{
    AL_PRE_COND(validate(index), );

    Rect rect;
    if (!src_rect)
        rect = image_.getRect();
    else
        rect = *src_rect;

    if (rects_[index].src_rect != rect)
    {
        rects_[index].src_rect = rect;
        setDirty(true);
    }
}

void ImageShape::setDest(uint32_t index, const Point2D& dst_xy)
{
    AL_PRE_COND(validate(index), );

    Rect dst_rect(rects_[index].src_rect);
    dst_rect.x = dst_xy.x;
    dst_rect.y = dst_xy.y;

    setDest(index, dst_rect);
}

void ImageShape::setDest(uint32_t index, const Rect& dst_rect)
{
    AL_PRE_COND(validate(index), );

    if (rects_[index].dst_rect != dst_rect)
    {
        rects_[index].dst_rect = dst_rect;
        setDirty(true);
    }
}

void ImageShape::setColor(uint32_t index, const Color& color)
{
    AL_PRE_COND(validate(index), );

    if (rects_[index].color != color)
    {
        rects_[index].color = color;
        setDirty(true);
    }
}

}
